home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / RNG.cc,v < prev    next >
Text File  |  1989-02-20  |  3KB  |  147 lines

  1. head     3.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    grunwald:3.2; strict;
  6. comment  @@;
  7.  
  8.  
  9. 3.2
  10. date     89.02.20.15.37.01;  author grunwald;  state Exp;
  11. branches ;
  12. next     3.1;
  13.  
  14. 3.1
  15. date     88.12.20.13.49.05;  author grunwald;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     88.09.18.16.42.27;  author grunwald;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 3.2
  30. log
  31. @Start using Gnu library heaps for schedulers
  32. @
  33. text
  34. @#include "RNG.h"
  35. #include <stream.h>
  36. #include "assert.h"
  37.  
  38. static char initialized = 0;
  39.  
  40. RNG::RNG()
  41. {
  42.     if (!initialized) {
  43.     if (sizeof(double) != 2 * sizeof(unsigned long)) {
  44.         cerr << "[Random] sizeof(Double) != 2 Sizeof(long)\n";
  45.         exit(1);
  46.     };
  47.     
  48.     double x = 1.0;
  49.     double y = 0.5;
  50.     PrivateRNGType t;
  51.  
  52.     //
  53.     //    The following is a hack that I attribute to
  54.     //    Andres Nowatzyk at CMU. The intent of the loop
  55.     //    is to form the smallest number 0 <= x < 1.0,
  56.     //    which is then used as a mask for two longwords.
  57.     //    this gives us a fast way way to produce double
  58.     //    precision numbers from longwords.
  59.     //
  60.     //    I know that this works for IEEE and VAX floating
  61.     //    point representations.
  62.     //
  63.     //    A further complication is that gnu C will blow
  64.     //    the following loop, unless compiled with -ffloat-store,
  65.     //    because it uses extended representations for some of
  66.     //    of the comparisons. Thus, we have the following hack.
  67.     //    If we could specify #pragma optimize, we wouldn't need this.
  68.     //
  69.  
  70. #ifndef NOT_IEEE
  71.     
  72.     t.d = 1.5;
  73.     if ( t.u[1] == 0 ) {        // sun word order?
  74.         t.u[0] = 0x3fffffff;
  75.         t.u[1] = 0xffffffff;
  76.     }
  77.     else {
  78.         t.u[0] = 0xffffffff;    // encore word order?
  79.         t.u[1] = 0x3fffffff;
  80.     }
  81. #else
  82.     do {                // find largest fp-number < 2.0
  83.         t.d = x;
  84.         x += y;
  85.         y *= 0.5;
  86.     } while (x != t.d && x < 2.0);
  87. #endif
  88.  
  89.     mantissa.d = 1.0;
  90.     mantissa.u[0] ^= t.u[0];
  91.     mantissa.u[1] ^= t.u[1]; // mantissa is now 1 for each mantissa bit
  92.  
  93.     initialized = 1;
  94.     }
  95. }
  96.  
  97. unsigned long
  98. RNG::asLong()
  99. {
  100.     assert2(0,"subClassResponsibility");
  101.     return(0);
  102. }
  103.  
  104. void
  105. RNG::reset()
  106. {
  107.     assert2(0,"subClassResponsibility");
  108. }
  109. @
  110.  
  111.  
  112. 3.1
  113. log
  114. @Steay version
  115. @
  116. text
  117. @d2 1
  118. d5 1
  119. a5 4
  120. //
  121. //    The scale constant is 2^-31. It is used to scale a 31 bit
  122. //    long to a double.
  123. //
  124. d7 11
  125. a17 2
  126. const double randomDoubleScaleConstant = 4.656612873077392578125e-10;
  127. const float  randomFloatScaleConstant = 4.656612873077392578125e-10;    
  128. d19 45
  129. a74 8
  130. }
  131.  
  132. float RNG::asFloat() {
  133.     return( (asLong() & 0x7fffffff) * randomFloatScaleConstant);
  134. }
  135.     
  136. double RNG::asDouble() {
  137.     return( (asLong() & 0x7fffffff) * randomDoubleScaleConstant);
  138. @
  139.  
  140.  
  141. 1.1
  142. log
  143. @Initial revision
  144. @
  145. text
  146. @@
  147.